package com.example.administrator.newxlmedical;

import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.text.NumberFormat;
import java.util.ArrayList;

import beans.CountGoodsDetailInfoBean;
import beans.GoodsDetailInfoBean;
import beans.GoodsStatisticsBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;
import utils.ActivityUtil;

public class CountGoodsDetialActivity extends BaseActivity {


    @BindView(R.id.iv_common_toolbar_home)
    ImageView ivHome;
    @BindView(R.id.iv_common_toolbar_back)
    ImageView ivBack;
    @BindView(R.id.tv_count_goods_detail_name)
    TextView tvName;
    @BindView(R.id.tv_count_goods_detail_specification)
    TextView tvSpecification;
    @BindView(R.id.tv_count_goods_detail_supplier)
    TextView tvSupplier;
    @BindView(R.id.tv_count_goods_detail_unit)
    TextView tvUnit;
    @BindView(R.id.tv_goods_detail_title_status)
    TextView tvGoodsDetailTitleStatus;
    @BindView(R.id.tv_count_goods_detail_star_data)
    TextView tvStarData;
    @BindView(R.id.tv_count_goods_detail_end_data)
    TextView tvEndData;
    @BindView(R.id.count_piechart_1)
    PieChart mChart;
    @BindView(R.id.count_piechart_stock)
    TextView countStock;
    @BindView(R.id.count_piechart_stock_sum)
    TextView countStockSum;
    @BindView(R.id.count_piechart_stock_percentage)
    TextView countStockPercentage;
    @BindView(R.id.count_piechart_2)
    PieChart chart2;
    @BindView(R.id.count_piechart_use)
    TextView countUse;
    @BindView(R.id.count_piechart_use_sum)
    TextView countUseSum;
    @BindView(R.id.count_piechart_use_percentage)
    TextView countUsePercentage;
    @BindView(R.id.count_piechart_3)
    PieChart chart3;
    @BindView(R.id.count_piechart_save)
    TextView countSave;
    @BindView(R.id.count_piechart_save_sum)
    TextView countSaveSum;
    @BindView(R.id.count_piechart_save_percentage)
    TextView countSavePercentage;
    @BindView(R.id.count_piechart_4)
    PieChart chart4;
    @BindView(R.id.count_piechart_take)
    TextView countTake;
    @BindView(R.id.count_piechart_take_sum)
    TextView countTakeSum;
    @BindView(R.id.count_piechart_take_percentage)
    TextView countTakePercentage;
    @BindView(R.id.count_piechart_5)
    PieChart chart5;
    @BindView(R.id.count_piechart_return)
    TextView countReturn;
    @BindView(R.id.count_piechart_return_sum)
    TextView countReturnSum;
    @BindView(R.id.count_piechart_return_percentage)
    TextView countReturnPercentage;
    private GoodsStatisticsBean.DataBean dataBean;
    private CountGoodsDetailInfoBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_goods_detial);
        ButterKnife.bind(this);
        Bundle bundle = (Bundle) getIntent().getExtras().get("bundle");
        bean = (CountGoodsDetailInfoBean) bundle.getSerializable(Constants.GOODS_DETIAL_INFO);
        dataBean = bean.getBean();
        init();
        showChart(mChart, getPieData(Color.rgb(79, 178, 92), Color.rgb(138, 155, 140), dataBean.getPurchaseCount(), bean.getSum1()));

        showChart(chart2, getPieData(Color.rgb(61, 88, 155), Color.rgb(123, 134, 161), dataBean.getApplyCount(), bean.getSum2()));

        showChart(chart3, getPieData(Color.rgb(55, 167, 233), Color.rgb(127, 174, 197), dataBean.getStockCount(), bean.getSum3()));

        showChart(chart4, getPieData(Color.rgb(255, 152, 30), Color.rgb(203, 176, 143), dataBean.getTakeOutCount(), bean.getSum4()));

        showChart(chart5, getPieData(Color.rgb(255, 51, 51), Color.rgb(207, 136, 136), dataBean.getNotReturnedCount(), bean.getSum5()));
    }

    public void init() {
        tvName.setText(dataBean.getGoodName());
        tvSpecification.setText(dataBean.getSpecification());
        tvSupplier.setText(dataBean.getManufacturer());
        tvUnit.setText(dataBean.getGoodsUnit());
        tvStarData.setText(bean.getStarTime());
        tvEndData.setText(bean.getEndTime());
        countStock.setText(String.valueOf(dataBean.getPurchaseCount()));
        countStockSum.setText(String.valueOf(bean.getSum1()));
        if (bean.getSum1() != 0) {
            countStockPercentage.setText(getPercentage(dataBean.getPurchaseCount(), bean.getSum1()) + "%");
        } else {
            countStockPercentage.setText("0%");
        }

        countUse.setText(String.valueOf(dataBean.getApplyCount()));
        countUseSum.setText(String.valueOf(bean.getSum2()));
        if (bean.getSum2() != 0) {
            countUsePercentage.setText(getPercentage(dataBean.getApplyCount(), bean.getSum2()) + "%");
        } else {
            countUsePercentage.setText("0%");
        }

        countSave.setText(String.valueOf(dataBean.getStockCount()));
        countSaveSum.setText(String.valueOf(bean.getSum3()));
        if (bean.getSum3() != 0) {
            countSavePercentage.setText(getPercentage(dataBean.getStockCount(), bean.getSum3()) + "%");
        } else {
            countSavePercentage.setText("0%");
        }

        countTake.setText(String.valueOf(dataBean.getTakeOutCount()));
        countTakeSum.setText(String.valueOf(bean.getSum4()));
        if (bean.getSum4() != 0) {
            countTakePercentage.setText(getPercentage(dataBean.getTakeOutCount(), bean.getSum4()) + "%");
        } else {
            countTakePercentage.setText("0%");
        }

        countReturn.setText(String.valueOf(dataBean.getNotReturnedCount()));
        countReturnSum.setText(String.valueOf(bean.getSum5()));
        if (bean.getSum5() != 0) {
            countReturnPercentage.setText(getPercentage(dataBean.getNotReturnedCount(), bean.getSum5()) + "%");
        } else {
            countReturnPercentage.setText("0%");
        }

    }

    private String getPercentage(int a, int b) {

        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();

        // 设置精确到小数点后2位

        numberFormat.setMaximumFractionDigits(2);

        return numberFormat.format((float) a / (float) b * 100);
    }

    private void showChart(PieChart chart, PieData pieData) {
        chart.setHoleRadius(0);  // 实心圆
        chart.setDrawHoleEnabled(false);
        chart.setRotationAngle(-60); // 初始旋转角度
        chart.setRotationEnabled(false); // 可以手动旋转
        // mChart.setUsePercentValues(true);  //显示成百分比
        chart.setDrawEntryLabels(false);
        // 设置可触摸
        chart.setTouchEnabled(true);
        Description description = new Description();
        description.setText("");
        chart.setDescription(description);
        // 设置数据
        chart.setData(pieData);
        chart.getDescription().setEnabled(false);
        // 取消高亮显示
//        mChart.highlightValues(null);
        chart.invalidate();

        //设置动画
        chart.animateXY(1000, 1000);
    }

    private PieData getPieData(int one, int two, int number, int sum) {
        // yVals用来表示封装每个饼块的实际数据
        ArrayList<PieEntry> yValues = new ArrayList<PieEntry>();

        // 饼图数据
        float quarterly1;
        float quarterly2;

        if (number == sum) {
            quarterly1 = 1;
            quarterly2 = 0;
        } else {
            quarterly1 = Float.valueOf(getPercentage(number, sum));
            quarterly2 = 100 - Float.valueOf(getPercentage(number, sum));
        }

        yValues.add(new PieEntry(quarterly1, 0));
        yValues.add(new PieEntry(quarterly2, 1));

        // y轴集合
        PieDataSet pieDataSet = new PieDataSet(yValues, "");
        pieDataSet.setSliceSpace(2f); //设置个饼状图之间的距离

        ArrayList<Integer> colors = new ArrayList<Integer>();

        // 饼图颜色
        colors.add(one);
        colors.add(two);

        // 设置饼图颜色
        pieDataSet.setColors(colors);

        // 设置选中态多出的长度
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float px = 5 * (metrics.densityDpi / 160f);
        pieDataSet.setSelectionShift(px);

        // 创建饼图数据
        PieData pieData = new PieData(pieDataSet);
        pieData.setDrawValues(false);
        return pieData;
    }

    @OnClick({R.id.iv_common_toolbar_home, R.id.iv_common_toolbar_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_common_toolbar_home:
                ActivityUtil.jumpToActivity(this, MainActivity.class);
                finish();
                break;
            case R.id.iv_common_toolbar_back:
                finish();
                break;
        }
    }
}
